package best.driver.android.Activity.Register.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import best.driver.android.Beans.SelectCarItemInfo;
import best.driver.android.R;
import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.*;

public class SelectCarAdapter extends BaseAdapter implements SectionIndexer {

    HashMap<String, Integer> alphaIndexer;

    String[] sections;

    Context mContext;

    private List<SelectCarItemInfo> mUserItems = new ArrayList<SelectCarItemInfo>();

    public SelectCarAdapter(Context context, List<SelectCarItemInfo> items) {
        mContext = context;
        mUserItems = items;
        alphaIndexer = new HashMap<String, Integer>();
        int size = items.size();

        for(int x= 0; x < 26; x++) {
            char c= (char)('A' + x);
            alphaIndexer.put(String.valueOf(c), -1);
        }


        for (int x = 0; x < size; x++) {
            SelectCarItemInfo s = items.get(x);

            String ch = s.mType;
            if (!TextUtils.isEmpty(ch)) {
                ch = ch.toUpperCase().substring(0,1);
                if(alphaIndexer.get(ch) == -1)
                alphaIndexer.put(ch, x);
            } else {
                alphaIndexer.put("#", x);
            }
        }

        Set<String> sectionLetters = alphaIndexer.keySet();

        ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);

        Collections.sort(sectionList);

        sections = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N",
                "O","P","Q","R","S","T","U","V","W","X","Y","Z"};;

        sectionList.toArray(sections);
    }



    @Override
    public int getCount() {
        return mUserItems.size();
    }

    @Override
    public SelectCarItemInfo getItem(int position) {
        if(mUserItems.size() == 0)
            return null;
        return mUserItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_select_car, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        final SelectCarItemInfo friendItem = getItem(position);
        int prePositionSection = getSectionForPosition(position - 1);
        int currentSection = getSectionForPosition(position);
        int nextPositionSection = -1;
        if(position < getCount() - 1) {
            nextPositionSection = getSectionForPosition(position + 1);
        }





    }

    public int getPositionForSection(int section) {

        if (section < 0) {
            return -1;
        }
        if(section >= sections.length) {
            section = sections.length - 1;
        }
        return alphaIndexer.get(sections[section]);
    }

    public int getSectionForPosition(int position) {
        if (position < 0 || getItem(position) == null) {
            return -1;
        }
        int i;
        String sortKey = getItem(position).mType;
        for (i = 0; i < sections.length; i++) {
            if (sections[i].equals(sortKey)) {
                break;
            }
        }
        return i;
    }

    public Object[] getSections() {
        return sections;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_friend.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Inmite Developers (http://inmite.github.io)
     */
    static class ViewHolder {

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }


}
